﻿#region Libraries

using System;
using System.Drawing;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace XYZ_Inventory
{
    public partial class RawMaterials : Page
    {
        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            txtSearch.Text = "";
            GridView1.DataBind();
        }

        protected void Button2_OnClick_(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string extenstion = Path.GetExtension(FileUpload1.FileName);

                if (extenstion != null && extenstion.ToLower() == ".csv")
                {
                    using (StreamReader reader = new StreamReader(FileUpload1.FileContent))
                    {
                        XYZDBContext db = new XYZDBContext();

                        do
                        {
                            string line = reader.ReadLine();
                            if (line != null)
                            {
                                string[] data = line.Split(',');
                                RawMaterialInventory rawMaterial = new RawMaterialInventory
                                {
                                    RawMaterialName = data[0],
                                    RawMaterialType = data[1],
                                    Description = data[2]
                                };

                                float price;
                                if (float.TryParse(data[3], out price))
                                {
                                    rawMaterial.UnitBuyingPrice = price;
                                }

                                int reorder;
                                if (int.TryParse(data[4], out reorder))
                                {
                                    rawMaterial.ReorderLevel = reorder;
                                }

                                int stock;
                                if (int.TryParse(data[5], out stock))
                                {
                                    rawMaterial.Stock = stock;
                                }

                                db.RawMaterialInventories.AddObject(rawMaterial);
                                db.SaveChanges();

                                lblImport.Text = "Upload success";
                                lblImport.ForeColor = Color.Green;
                            }
                        } while (reader.Peek() != -1);

                        GridView1.DataBind();
                    }
                }
                else
                {
                    lblImport.Text = "Please upload only a CSV file";
                    lblImport.ForeColor = Color.Red;
                }
            }
            else
            {
                lblImport.Text = "Please upload a CSV file";
                lblImport.ForeColor = Color.Red;
            }
        }
    }
}